<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.16"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>ClaraGenomicsAnalysis: common/utils/include/claragenomics/utils/cudautils.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">ClaraGenomicsAnalysis
   &#160;<span id="projectnumber">0.4.0</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.16 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_bdd9a5d540de89e9fe90efdfc6973a4f.html">common</a></li><li class="navelem"><a class="el" href="dir_85effdddc426fd82e3610be0948fd413.html">utils</a></li><li class="navelem"><a class="el" href="dir_cbd0fd5fcc23422502de88d4fe6e7386.html">include</a></li><li class="navelem"><a class="el" href="dir_8e216217187728c84aa14a4942da12a2.html">claragenomics</a></li><li class="navelem"><a class="el" href="dir_657c9b9511f57ce884717ee8b069b43d.html">utils</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">cudautils.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="cudautils_8hpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment">* Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment">*</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment">* NVIDIA CORPORATION and its licensors retain all intellectual property</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment">* and proprietary rights in and to this software, related documentation</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment">* and any modifications thereto.  Any use, reproduction, disclosure or</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment">* distribution of this software and related documentation without an express</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment">* license agreement from NVIDIA CORPORATION is strictly prohibited.</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment">*/</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160; </div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="preprocessor">#pragma once</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160; </div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="preprocessor">#include &lt;claragenomics/utils/cudaversions.hpp&gt;</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="logging_8hpp.html">claragenomics/logging/logging.hpp</a>&gt;</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160; </div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="preprocessor">#include &lt;cuda_runtime_api.h&gt;</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#include &lt;cassert&gt;</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160; </div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#ifdef CGA_PROFILING</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="preprocessor">#include &lt;nvToolsExt.h&gt;</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="preprocessor">#endif // CGA_PROFILING</span></div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160; </div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160; </div>
<div class="line"><a name="l00031"></a><span class="lineno"><a class="line" href="group__cudautils.html#ga161535c48fe399e086ac6277b527fb41">   31</a></span>&#160;<span class="preprocessor">#define CGA_CU_CHECK_ERR(ans)                                            \</span></div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;<span class="preprocessor">    {                                                                    \</span></div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;<span class="preprocessor">        claragenomics::cudautils::gpu_assert((ans), __FILE__, __LINE__); \</span></div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;<span class="preprocessor">    }</span></div>
<div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160; </div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160; </div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;<span class="keyword">namespace </span>claragenomics</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;{</div>
<div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160; </div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;<span class="keyword">namespace </span>cudautils</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;{</div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160; </div>
<div class="line"><a name="l00050"></a><span class="lineno"><a class="line" href="group__cudautils.html#ga3769a812992df47886894f48f26edf6f">   50</a></span>&#160;<span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="group__cudautils.html#ga3769a812992df47886894f48f26edf6f">gpu_assert</a>(cudaError_t code, <span class="keyword">const</span> <span class="keywordtype">char</span>* file, <span class="keywordtype">int</span> line)</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;{</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;<span class="preprocessor">#ifdef CGA_DEVICE_SYNCHRONIZE</span></div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;    <span class="comment">// This device synchronize forces the most recent CUDA call to fully</span></div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;    <span class="comment">// complete, increasing the chance of catching the CUDA error near the</span></div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;    <span class="comment">// offending function. Only run if existing code is success to avoid</span></div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;    <span class="comment">// potentially overwriting previous error code.</span></div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;    <span class="keywordflow">if</span> (code == cudaSuccess)</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;    {</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;        code = cudaDeviceSynchronize();</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;    }</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160; </div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;    <span class="keywordflow">if</span> (code != cudaSuccess)</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;    {</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;        std::string err = <span class="stringliteral">&quot;GPU Error:: &quot;</span> +</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;                          std::string(cudaGetErrorString(code)) +</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;                          <span class="stringliteral">&quot; &quot;</span> + std::string(file) +</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;                          <span class="stringliteral">&quot; &quot;</span> + std::to_string(line);</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;        <a class="code" href="group__logging.html#gacd5f9588e4976ef45d2ddece37b78003">CGA_LOG_ERROR</a>(<span class="stringliteral">&quot;{}\n&quot;</span>, err);</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;        <span class="comment">// In Debug mode, this assert will cause a debugger trap</span></div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;        <span class="comment">// which is beneficial when debugging errors.</span></div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;        assert(<span class="keyword">false</span>);</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;        std::abort();</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;    }</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;}</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160; </div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;<span class="keyword">template</span> &lt;<span class="keyword">typename</span> IntType, <span class="keywordtype">int</span>32_t boundary&gt;</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;__host__ __device__ __forceinline__</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;    IntType</div>
<div class="line"><a name="l00086"></a><span class="lineno"><a class="line" href="cudautils_8hpp.html#a9ace0fde2c5420f52b40ca77276943ca">   86</a></span>&#160;    <a class="code" href="cudautils_8hpp.html#a9ace0fde2c5420f52b40ca77276943ca">align</a>(<span class="keyword">const</span> IntType&amp; value)</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;{</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;    static_assert((boundary &amp; (boundary - 1)) == 0, <span class="stringliteral">&quot;Boundary for align must be power of 2&quot;</span>);</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    <span class="keywordflow">return</span> (value + boundary) &amp; ~(boundary - 1);</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;}</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160; </div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;<span class="preprocessor">#ifdef CGA_PROFILING</span></div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;<span class="preprocessor">#define CGA_NVTX_RANGE(varname, label) ::claragenomics::cudautils::nvtx_range varname(label)</span></div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;<span class="keyword">class </span>nvtx_range</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;{</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;    <span class="keyword">explicit</span> nvtx_range(<span class="keywordtype">char</span> <span class="keyword">const</span>* name)</div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;    {</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;        nvtxRangePush(name);</div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;    }</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160; </div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;    ~nvtx_range()</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;    {</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;        nvtxRangePop();</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;    }</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;};</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;<span class="preprocessor">#else</span></div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;<span class="preprocessor">#define CGA_NVTX_RANGE(varname, label)</span></div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;<span class="preprocessor">#endif // CGA_PROFILING</span></div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160; </div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;} <span class="comment">// namespace cudautils</span></div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160; </div>
<div class="line"><a name="l00130"></a><span class="lineno"><a class="line" href="classclaragenomics_1_1scoped__device__switch.html">  130</a></span>&#160;<span class="keyword">class </span><a class="code" href="classclaragenomics_1_1scoped__device__switch.html">scoped_device_switch</a></div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;{</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00136"></a><span class="lineno"><a class="line" href="classclaragenomics_1_1scoped__device__switch.html#a41381ff64aeb0750803a315020a13650">  136</a></span>&#160;    <span class="keyword">explicit</span> <a class="code" href="classclaragenomics_1_1scoped__device__switch.html#a41381ff64aeb0750803a315020a13650">scoped_device_switch</a>(int32_t device_id)</div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;    {</div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;        <a class="code" href="group__cudautils.html#ga161535c48fe399e086ac6277b527fb41">CGA_CU_CHECK_ERR</a>(cudaGetDevice(&amp;device_id_before_));</div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;        <a class="code" href="group__cudautils.html#ga161535c48fe399e086ac6277b527fb41">CGA_CU_CHECK_ERR</a>(cudaSetDevice(device_id));</div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;    }</div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160; </div>
<div class="line"><a name="l00143"></a><span class="lineno"><a class="line" href="classclaragenomics_1_1scoped__device__switch.html#ad17a140b9fc9badff82487e685013125">  143</a></span>&#160;    <a class="code" href="classclaragenomics_1_1scoped__device__switch.html#ad17a140b9fc9badff82487e685013125">~scoped_device_switch</a>()</div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;    {</div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;        cudaSetDevice(device_id_before_);</div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;    }</div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160; </div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;    <a class="code" href="classclaragenomics_1_1scoped__device__switch.html">scoped_device_switch</a>()                            = <span class="keyword">delete</span>;</div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;    <a class="code" href="classclaragenomics_1_1scoped__device__switch.html">scoped_device_switch</a>(<a class="code" href="classclaragenomics_1_1scoped__device__switch.html">scoped_device_switch</a> <span class="keyword">const</span>&amp;) = <span class="keyword">delete</span>;</div>
<div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;    <a class="code" href="classclaragenomics_1_1scoped__device__switch.html">scoped_device_switch</a>&amp; operator=(<a class="code" href="classclaragenomics_1_1scoped__device__switch.html">scoped_device_switch</a> <span class="keyword">const</span>&amp;) = <span class="keyword">delete</span>;</div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160; </div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;<span class="keyword">private</span>:</div>
<div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;    int32_t device_id_before_;</div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;};</div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160; </div>
<div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;} <span class="comment">// namespace claragenomics</span></div>
</div><!-- fragment --></div><!-- contents -->
<div class="ttc" id="agroup__cudautils_html_ga161535c48fe399e086ac6277b527fb41"><div class="ttname"><a href="group__cudautils.html#ga161535c48fe399e086ac6277b527fb41">CGA_CU_CHECK_ERR</a></div><div class="ttdeci">#define CGA_CU_CHECK_ERR(ans)</div><div class="ttdoc">Log on CUDA error in enclosed expression.</div><div class="ttdef"><b>Definition:</b> cudautils.hpp:31</div></div>
<div class="ttc" id="agroup__logging_html_gacd5f9588e4976ef45d2ddece37b78003"><div class="ttname"><a href="group__logging.html#gacd5f9588e4976ef45d2ddece37b78003">CGA_LOG_ERROR</a></div><div class="ttdeci">#define CGA_LOG_ERROR(...)</div><div class="ttdoc">Log at error level.</div><div class="ttdef"><b>Definition:</b> logging.hpp:164</div></div>
<div class="ttc" id="aclassclaragenomics_1_1scoped__device__switch_html_a41381ff64aeb0750803a315020a13650"><div class="ttname"><a href="classclaragenomics_1_1scoped__device__switch.html#a41381ff64aeb0750803a315020a13650">claragenomics::scoped_device_switch::scoped_device_switch</a></div><div class="ttdeci">scoped_device_switch(int32_t device_id)</div><div class="ttdoc">Constructor.</div><div class="ttdef"><b>Definition:</b> cudautils.hpp:136</div></div>
<div class="ttc" id="aclassclaragenomics_1_1scoped__device__switch_html_ad17a140b9fc9badff82487e685013125"><div class="ttname"><a href="classclaragenomics_1_1scoped__device__switch.html#ad17a140b9fc9badff82487e685013125">claragenomics::scoped_device_switch::~scoped_device_switch</a></div><div class="ttdeci">~scoped_device_switch()</div><div class="ttdoc">Destructor switches back to original device ID.</div><div class="ttdef"><b>Definition:</b> cudautils.hpp:143</div></div>
<div class="ttc" id="aclassclaragenomics_1_1scoped__device__switch_html"><div class="ttname"><a href="classclaragenomics_1_1scoped__device__switch.html">claragenomics::scoped_device_switch</a></div><div class="ttdoc">A class to switch the CUDA device for the current scope using RAII.</div><div class="ttdef"><b>Definition:</b> cudautils.hpp:130</div></div>
<div class="ttc" id="alogging_8hpp_html"><div class="ttname"><a href="logging_8hpp.html">logging.hpp</a></div></div>
<div class="ttc" id="agroup__cudautils_html_ga3769a812992df47886894f48f26edf6f"><div class="ttname"><a href="group__cudautils.html#ga3769a812992df47886894f48f26edf6f">claragenomics::cudautils::gpu_assert</a></div><div class="ttdeci">void gpu_assert(cudaError_t code, const char *file, int line)</div><div class="ttdef"><b>Definition:</b> cudautils.hpp:50</div></div>
<div class="ttc" id="acudautils_8hpp_html_a9ace0fde2c5420f52b40ca77276943ca"><div class="ttname"><a href="cudautils_8hpp.html#a9ace0fde2c5420f52b40ca77276943ca">claragenomics::cudautils::align</a></div><div class="ttdeci">__host__ __device__ __forceinline__ IntType align(const IntType &amp;value)</div><div class="ttdef"><b>Definition:</b> cudautils.hpp:86</div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.16
</small></address>
</body>
</html>
